package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Exame;

import com.model.ManutencoesCulm;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table MANUTENCOES_CULM.
*/
public class ManutencoesCulmDao extends AbstractDao<ManutencoesCulm, Long> {

    public static final String TABLENAME = "MANUTENCOES_CULM";

    /**
     * Properties of entity ManutencoesCulm.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, long.class, "id", true, "_idManu");
        public final static Property Data = new Property(1, java.util.Date.class, "data", false, "DATA");
        public final static Property Ano = new Property(2, String.class, "ano", false, "ANO");
        public final static Property Numcolm = new Property(3, String.class, "numcolm", false, "NUMCOLM");
        public final static Property Tipointrev = new Property(4, Integer.class, "tipointrev", false, "TIPOINTREV");
        public final static Property Descricao = new Property(5, String.class, "descricao", false, "DESCRICAO");
        public final static Property Temperatura = new Property(6, String.class, "temperatura", false, "TEMPERATURA");
        public final static Property Humidade = new Property(7, String.class, "humidade", false, "HUMIDADE");
        public final static Property Doencadetct = new Property(8, Integer.class, "doencadetct", false, "DOENCADETCT");
        public final static Property Trafego = new Property(9, Integer.class, "trafego", false, "TRAFEGO");
        public final static Property Padraopostura = new Property(10, Integer.class, "padraopostura", false, "PADRAOPOSTURA");
        public final static Property NCupReais = new Property(11, Integer.class, "nCupReais", false, "N_CUP_REAIS");
        public final static Property NOvosdia = new Property(12, Integer.class, "nOvosdia", false, "N_OVOSDIA");
        public final static Property Entrapolen = new Property(13, Integer.class, "entrapolen", false, "ENTRAPOLEN");
        public final static Property NivReserv = new Property(14, Integer.class, "nivReserv", false, "NIV_RESERV");
        public final static Property ApiarioId = new Property(15, long.class, "apiarioId", false, "APIARIO_ID");
        public final static Property ColmeiaId = new Property(16, long.class, "colmeiaId", false, "COLMEIA_ID");
        public final static Property Date = new Property(17, java.util.Date.class, "date", false, "DATE");
    };

    private DaoSession daoSession;

    private Query<ManutencoesCulm> apiario_ApiarioManuCulmeiaQuery;
    private Query<ManutencoesCulm> exame_ExameManuCulmQuery;

    public ManutencoesCulmDao(DaoConfig config) {
        super(config);
    }
    
    public ManutencoesCulmDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'MANUTENCOES_CULM' (" + //
                "'_idManu' INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL ," + // 0: id
                "'DATA' INTEGER," + // 1: data
                "'ANO' TEXT," + // 2: ano
                "'NUMCOLM' TEXT," + // 3: numcolm
                "'TIPOINTREV' INTEGER," + // 4: tipointrev
                "'DESCRICAO' TEXT," + // 5: descricao
                "'TEMPERATURA' TEXT," + // 6: temperatura
                "'HUMIDADE' TEXT," + // 7: humidade
                "'DOENCADETCT' INTEGER," + // 8: doencadetct
                "'TRAFEGO' INTEGER," + // 9: trafego
                "'PADRAOPOSTURA' INTEGER," + // 10: padraopostura
                "'N_CUP_REAIS' INTEGER," + // 11: nCupReais
                "'N_OVOSDIA' INTEGER," + // 12: nOvosdia
                "'ENTRAPOLEN' INTEGER," + // 13: entrapolen
                "'NIV_RESERV' INTEGER," + // 14: nivReserv
                "'APIARIO_ID' INTEGER NOT NULL ," + // 15: apiarioId
                "'COLMEIA_ID' INTEGER NOT NULL ," + // 16: colmeiaId
                "'DATE' INTEGER);"); // 17: date
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'MANUTENCOES_CULM'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, ManutencoesCulm entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        java.util.Date data = entity.getData();
        if (data != null) {
            stmt.bindLong(2, data.getTime());
        }
 
        String ano = entity.getAno();
        if (ano != null) {
            stmt.bindString(3, ano);
        }
 
        String numcolm = entity.getNumcolm();
        if (numcolm != null) {
            stmt.bindString(4, numcolm);
        }
 
        Integer tipointrev = entity.getTipointrev();
        if (tipointrev != null) {
            stmt.bindLong(5, tipointrev);
        }
 
        String descricao = entity.getDescricao();
        if (descricao != null) {
            stmt.bindString(6, descricao);
        }
 
        String temperatura = entity.getTemperatura();
        if (temperatura != null) {
            stmt.bindString(7, temperatura);
        }
 
        String humidade = entity.getHumidade();
        if (humidade != null) {
            stmt.bindString(8, humidade);
        }
 
        Integer doencadetct = entity.getDoencadetct();
        if (doencadetct != null) {
            stmt.bindLong(9, doencadetct);
        }
 
        Integer trafego = entity.getTrafego();
        if (trafego != null) {
            stmt.bindLong(10, trafego);
        }
 
        Integer padraopostura = entity.getPadraopostura();
        if (padraopostura != null) {
            stmt.bindLong(11, padraopostura);
        }
 
        Integer nCupReais = entity.getNCupReais();
        if (nCupReais != null) {
            stmt.bindLong(12, nCupReais);
        }
 
        Integer nOvosdia = entity.getNOvosdia();
        if (nOvosdia != null) {
            stmt.bindLong(13, nOvosdia);
        }
 
        Integer entrapolen = entity.getEntrapolen();
        if (entrapolen != null) {
            stmt.bindLong(14, entrapolen);
        }
 
        Integer nivReserv = entity.getNivReserv();
        if (nivReserv != null) {
            stmt.bindLong(15, nivReserv);
        }
        stmt.bindLong(16, entity.getApiarioId());
        stmt.bindLong(17, entity.getColmeiaId());
 
        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(18, date.getTime());
        }
    }

    @Override
    protected void attachEntity(ManutencoesCulm entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public ManutencoesCulm readEntity(Cursor cursor, int offset) {
        ManutencoesCulm entity = new ManutencoesCulm( //
            cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : new java.util.Date(cursor.getLong(offset + 1)), // data
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // ano
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // numcolm
            cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4), // tipointrev
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // descricao
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // temperatura
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // humidade
            cursor.isNull(offset + 8) ? null : cursor.getInt(offset + 8), // doencadetct
            cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9), // trafego
            cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10), // padraopostura
            cursor.isNull(offset + 11) ? null : cursor.getInt(offset + 11), // nCupReais
            cursor.isNull(offset + 12) ? null : cursor.getInt(offset + 12), // nOvosdia
            cursor.isNull(offset + 13) ? null : cursor.getInt(offset + 13), // entrapolen
            cursor.isNull(offset + 14) ? null : cursor.getInt(offset + 14), // nivReserv
            cursor.getLong(offset + 15), // apiarioId
            cursor.getLong(offset + 16), // colmeiaId
            cursor.isNull(offset + 17) ? null : new java.util.Date(cursor.getLong(offset + 17)) // date
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, ManutencoesCulm entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setData(cursor.isNull(offset + 1) ? null : new java.util.Date(cursor.getLong(offset + 1)));
        entity.setAno(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setNumcolm(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setTipointrev(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4));
        entity.setDescricao(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setTemperatura(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setHumidade(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setDoencadetct(cursor.isNull(offset + 8) ? null : cursor.getInt(offset + 8));
        entity.setTrafego(cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9));
        entity.setPadraopostura(cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10));
        entity.setNCupReais(cursor.isNull(offset + 11) ? null : cursor.getInt(offset + 11));
        entity.setNOvosdia(cursor.isNull(offset + 12) ? null : cursor.getInt(offset + 12));
        entity.setEntrapolen(cursor.isNull(offset + 13) ? null : cursor.getInt(offset + 13));
        entity.setNivReserv(cursor.isNull(offset + 14) ? null : cursor.getInt(offset + 14));
        entity.setApiarioId(cursor.getLong(offset + 15));
        entity.setColmeiaId(cursor.getLong(offset + 16));
        entity.setDate(cursor.isNull(offset + 17) ? null : new java.util.Date(cursor.getLong(offset + 17)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(ManutencoesCulm entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(ManutencoesCulm entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioManuCulmeia" to-many relationship of Apiario. */
    public List<ManutencoesCulm> _queryApiario_ApiarioManuCulmeia(long apiarioId) {
        synchronized (this) {
            if (apiario_ApiarioManuCulmeiaQuery == null) {
                QueryBuilder<ManutencoesCulm> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApiarioId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apiario_ApiarioManuCulmeiaQuery = queryBuilder.build();
            }
        }
        Query<ManutencoesCulm> query = apiario_ApiarioManuCulmeiaQuery.forCurrentThread();
        query.setParameter(0, apiarioId);
        return query.list();
    }

    /** Internal query to resolve the "exameManuCulm" to-many relationship of Exame. */
    public List<ManutencoesCulm> _queryExame_ExameManuCulm(long colmeiaId) {
        synchronized (this) {
            if (exame_ExameManuCulmQuery == null) {
                QueryBuilder<ManutencoesCulm> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ColmeiaId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                exame_ExameManuCulmQuery = queryBuilder.build();
            }
        }
        Query<ManutencoesCulm> query = exame_ExameManuCulmQuery.forCurrentThread();
        query.setParameter(0, colmeiaId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getExameDao().getAllColumns());
            builder.append(" FROM MANUTENCOES_CULM T");
            builder.append(" LEFT JOIN APIARIO T0 ON T.'APIARIO_ID'=T0.'_idApiario'");
            builder.append(" LEFT JOIN EXAME T1 ON T.'COLMEIA_ID'=T1.'_idExame'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ManutencoesCulm loadCurrentDeep(Cursor cursor, boolean lock) {
        ManutencoesCulm entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }

        return entity;    
    }

    public ManutencoesCulm loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ManutencoesCulm> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ManutencoesCulm> list = new ArrayList<ManutencoesCulm>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ManutencoesCulm> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ManutencoesCulm> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
